package com.aisile.crowd_funding.system.service.impl;

import com.aisile.crowd_funding.common.utils.PageUtils;
import com.aisile.crowd_funding.common.utils.Query;
import com.aisile.crowd_funding.common.utils.StringUtils;
import com.aisile.crowd_funding.system.mapper.SysUserMapper;
import com.aisile.crowd_funding.system.mapper.SysUserRoleMapper;
import com.aisile.crowd_funding.system.pojo.SysUser;
import com.aisile.crowd_funding.system.pojo.SysUserExample;
import com.aisile.crowd_funding.system.pojo.SysUserRole;
import com.aisile.crowd_funding.system.pojo.SysUserRoleExample;
import com.aisile.crowd_funding.system.service.UserService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @标题 UserServiceImpl
 * @作者 Mr.chen
 * @创建日期 2018/12/5 16:43
 * @版本号 1.0
 **/
@Service
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;
    @Override
    public int deleteByPrimaryKey(Long id) {
        int i=sysUserMapper.deleteByPrimaryKey(id);
        SysUserRoleExample sysUserRoleExample=new SysUserRoleExample();
        sysUserRoleExample.createCriteria().andUserIdEqualTo(id);
        sysUserRoleMapper.deleteByExample(sysUserRoleExample);
        return i;
    }

    @Override
    public List<Long> getRoleIdList(Long id) {
        List<Long> lo=sysUserRoleMapper.getRoleIdList(id);
        return lo;
    }

    @Override
    public int insert(SysUser record) {
        int i=sysUserMapper.insert(record);

        Long[] ids=record.getRoleIds();
        SysUserRole sysUserRole=new SysUserRole();
        for(Long id:ids){
           sysUserRole.setRoleId(id);
           sysUserRole.setUserId(record.getUserId());
        }
        sysUserRoleMapper.insert(sysUserRole);
        return i;
    }

    @Override
    public SysUser selectByPrimaryKey(Long id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageUtils slectByPage(Map params) {
        Query.startPage(params);
        String name=(String)params.get("name");
        String username=(String)params.get("username");
        SysUserExample example=new SysUserExample();
        if(!StringUtils.isBlank(name)){
            example.createCriteria().andNameLike("%"+name+"%");
        }
        if(!StringUtils.isBlank(username)){
            example.createCriteria().andUsernameLike("%"+username+"%");
        }
        Page<SysUser> page=(Page)sysUserMapper.selectByExample(null);
       PageUtils pageUtils=new PageUtils(page.getResult(),page.getTotal());
        return pageUtils;
    }

    @Override
    public int delete(Long[] ids) {
        int i=0;
        for(Long id:ids){
            i=sysUserMapper.deleteByPrimaryKey(id);
            //删除user表的同时删除关联数据
            SysUserRoleExample sysUserRoleExample=new SysUserRoleExample();
            sysUserRoleExample.createCriteria().andUserIdEqualTo(id);
            sysUserRoleMapper.deleteByExample(sysUserRoleExample);
        }

        return i;
    }

    @Override
    public int updateByPrimaryKey(SysUser sysUser) {
          int i=sysUserMapper.updateByPrimaryKey(sysUser);
          SysUserRoleExample example=new SysUserRoleExample();
          example.createCriteria().andUserIdEqualTo(sysUser.getUserId());
        Long[] ids=sysUser.getRoleIds();
        SysUserRole sysUserRole=new SysUserRole();
        for(Long id:ids){
            sysUserRole.setRoleId(id);
            sysUserRole.setUserId(sysUser.getUserId());
        }
        sysUserRoleMapper.insert(sysUserRole);
        return i;
    }

    @Override
    public List<SysUser> selectByExample(SysUserExample sysUserExample) {
        return sysUserMapper.selectByExample(sysUserExample);
    }

    @Override
    public SysUser findByUserName(String userName) {
        SysUserExample example=new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(userName);
        List<SysUser> list=sysUserMapper.selectByExample(example);
        if(list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }
}
